package com.chejianer.phone.domain;

import com.chejianer.phone.utils.CreateNonce;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.chejianer.phone.utils.CheckSumBuilder;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

/**
 * Created by Chejianer on 2016/8/25.
 */
public class CreatePhoneLine {
    public static String PhonePost(String callerAcc,String caller,String callee) throws Exception{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "https://api.netease.im/call/ecp/startcall.action";
        HttpPost httpPost = new HttpPost(url);

        /**
         * appkey与appSecret是你注册网易云信并创建的应用的对应d的内容
         * 登录网易云信可在你的项目管理上看到
         */
        String appKey = "your key";
        String appSecret = "your secret";
        String nonce= CreateNonce.getNonce();
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码

        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //nvps.add(new BasicNameValuePair("accid", "chejianer"));
        nvps.add(new BasicNameValuePair("callerAcc",callerAcc));
        nvps.add(new BasicNameValuePair("caller",caller));
        nvps.add(new BasicNameValuePair("callee",callee));
        nvps.add(new BasicNameValuePair("maxDur","3600"));


        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);

        String result=EntityUtils.toString(response.getEntity(), "utf-8");
        // 打印执行结果
        System.out.println(result);

        JSONObject jb=JSONObject.fromObject(result);
        //为了比较省事，将注册用户的工作在这里进行
        //如果返回414状态码并且提示callerAcc不正确，执行注册操作
        //判断顺序不能颠倒，因为只有状态码不是200的时候返回的json数据里才有msg。否则会报错找不到msg
        if("414".equals(jb.getString("code"))&&"bad param 'callerAcc'".equals(jb.getString("msg"))){
            String createAccid=CreateAccid.createId(callerAcc);
            JSONObject accid=JSONObject.fromObject(createAccid);
            if("200".equals(accid.getString("code"))){
                return PhonePost(callerAcc,caller,callee);
            }
            return result;
        }
            return result;

        //JSONArray jsonResult=JSONArray.fromObject(result);
       // return result;

    }
}
